package com.wangyi.fightimg.utils.http;

import android.content.Context;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author wangYi
 *         访问服务器回调
 */
public abstract class NetResponse<T> implements Callback<T>, NetWorkCallInterface {

    public Context context;
    public String fromUrl;

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        ResponseBody responseBody = (ResponseBody) response.body();
        try {
            String json = responseBody.string();
            try {
                JSONObject jsonObject = new JSONObject(json);
                switch (jsonObject.optInt("code")) {
                    case 0:
                        succeed( jsonObject);
                        break;
                    default:
                        fail(jsonObject.optString("msg"));
                        break;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {

    }

    public void showDialog() {

    }


}
